<?php

/**
 * AdminUser
 * 
 * @author Administrator
 * @version 
 */

require_once 'Zend/Db/Table/Abstract.php';
register_shutdown_function(myshutdown);
ini_set('display_errors', 'on');
function mydbg($a)
{
error_log("dbg:" . $a);
}
function myshutdown()
{

	error_log("i die!");
}
class Model_Backend_ERpdatabase extends Zend_Db_Table_Abstract {
	/**
	 * The default table name 
	 */
	protected $_name = null;
	public $arr_key = array();
	public $arr_value = array();
	public function setDatabaseName($databasename) {
		$this->_name = $databasename;
	}
	public function fillDataErp($arr, $gid) {
		$arr = $arr + array ('SPID' => $gid );
		if ($this->insert ( $arr )) {
			return true;
		} else
			return false;
	}
	public function checkDataErp($filed, $primaryKey) {
		if ($primaryKey == "") {
			return "primarykeyIsnull";
		} else
			return "CheckOk";
	}
	public function clearTableData($gid) {
		error_reporting(E_ALL);
		error_log("clearTableData start:" . $gid);
		error_log($this->_name);
		$select = "SPID = '" . $gid . "'";
		error_log($select);
		error_log("sleep 10 sec start");
		sleep(10);
		error_log("i wake up");
		$result = $this->fetchRow($select);
		error_log("fetchRow ok");
		$data = $result->toArray();
		error_log(count($data));
		if (count($data)>0)
		{
			if ($this->delete($select))
			{
				return true;
			}
			return false;
		}
		else 
		{
			return true;
		}
	}
	//获取订单相信信息
	public function getListOrder($key) {
		if ($key == "") {
			return;
		}
		if (is_array ( $key )) 
		{
			$key = implode ( ",", $key );
		}		
		$obj_select = $this->select ();
		$obj_select->setIntegrityCheck ( false );
		if ($this->_name == "MK_Evaluation_Main") {
			
			$obj_select->where ( "ID = '" . $key . "'" );
		}
		if ($this->_name == "MK_Evaluation_PicText")
		{
			$obj_select->where ( "BbqID = '" . $key . "'" );
		}
		if ($this->_name == "MK_Evaluation_Sub") {
			$obj_select->where ( "BbqID = '" . $key . "'" );
		}
		if ($this->_name == "MK_Evaluation_Component_Sub") {
			$obj_select->where ( "SubID  IN (" . $key . ")" );
		}
		if ($this->_name == "BBQ_User_Files") {
			$obj_select->where ( "nId  IN (" . $key . ")" );
		}
		if ($this->_name == "BBQ_Files") {
			//var_dump($key);
			$arr = explode ( ',', $key );
			
			$strMd5 = '';
			foreach ( $arr as $item ) {
				$strMd5 .= "'$item'" . ',';
			}
			$strMd5 = rtrim ( $strMd5, ',' );
			$obj_select->where ( "cMD5  IN (" . $strMd5 . ")" );
			//echo "cMD5  IN (" . $strMd5 . ")";
		//exit;
		}
		
		$result = $this->fetchAll ( $obj_select )->toArray ();
		return $result;
	}
	//获取表字段名称
	public function getTableColumns() {
		return $this->_getCols ();
	}
	//获取表table的xml结构
	public function getTableXML($key) {
		$xml = "<table>
						 <name>" . $this->_name . "</name>
						 	<data>
						 		<field_list>";
		$field_list = "";
		$allkey = array ();
		$allkey = $this->getTableColumns ();
		//取表字段
		foreach ( $allkey as $id => $item ) {
			$num = $id + 1;
			$field_list = $field_list . "<fie1d" . $num . ">" . $item . "</fie1d" . $num . ">";
		}
		if ($this->_name == "MK_Evaluation_Main"){
			//转换WebMobile->CustomerTel
			$field_list = str_replace('WebMobile','CustomerTel',$field_list);
			$num = $num + 1;
			$a = $num + 1;
			$b = $num + 2;
			$xml = $xml . $field_list . "<fie1d" . $num . ">" . SrcFilename . "</fie1d" . $num . ">" . "<fie1d" . $a . ">" . CompanyName . "</fie1d" . $a . ">" . "<fie1d" . $b . ">" . CustemerTel . "</fie1d" . $b . ">" . "</field_list><rec_list>";
		}
		else {
			$xml = $xml . $field_list . "</field_list><rec_list>";
		}		
		unset ( $allkey );
		//begin    取表记录集
		$data_parem = "";
		
		$result = array ();
		//取表数据集
		$result = $this->getListOrder ( $key );
		$num_datalist = count($result);
		for($i = 0; $i < $num_datalist; $i ++) {
			$j = 1;
			$data_list = "";
			foreach ( $result [$i] as $parem => $data ) {
				$data_list = $data_list . "<f" . $j . ">" . $data . "</f" . $j . ">";
				$j ++;
			}
			$this->setPrimaryKey ( $result [$i] );
			if ($this->_name == "MK_Evaluation_Main"){	
				$data_list = $data_list . "<f" . $j . "></f" . $j . ">" . "<f" . $a . "></f" . $a . ">" . "<f" . $b . "></f" . $b . ">";
			}
			$data_parem = $data_parem . "<rec>" . $data_list . "</rec>";
		}
		unset ( $result );
		//end
		$xml = $xml . $data_parem . "</rec_list></data></table>";
		return $xml;
	
	}
	
	public function getOrderXML($orderid,$value='') {
		//begin    取表记录集
		$result = array ();
		$result = $this->getListOrder ( $orderid );
		$data_list = "";
		$file_list = "";
		$readme_order = "";
		$file_name = array();
		$objUrl = new Model_Customize_CustFuns ();
		$rooturl = $objUrl->baseUrl ();
		$host = isset ( $_SERVER ['HTTP_X_FORWARDED_HOST'] ) ? $_SERVER ['HTTP_X_FORWARDED_HOST'] : (isset ( $_SERVER ['HTTP_HOST'] ) ? $_SERVER ['HTTP_HOST'] : '');
		$host = 'http://' . $host;
		for($i = 0; $i < count ( $result ); $i ++) {
			$j= '0';
			if (($this->_name == "MK_Evaluation_Main") || ($this->_name == "BBQ_User_Files") || ($this->_name == "BBQ_Files")) {
				if ($this->_name == "MK_Evaluation_Main") {
					if (empty($result [$i]['BBQOrderID']) || $result [$i]['BBQOrderID'] == null)
					{
						break;
					}					
						/*$data_list = $data_list . "<child_order>" . $result [$i]['BBQOrderID'] . "</child_order><id>" . $result [$i]['UserID'] . "</id><date>" . $result [$i]['CarryOnTim'] . "</date><product_name>" . $result [$i]['BillType'] . "</product_name><files>";
					    $readme_order = $readme_order . "<file><url>" . $host . $rooturl . "/orderinfo/createtxt/order/" . $result [$i]['BBQOrderID'] . "</url><file_name>" . $result [$i]['BBQOrderID'] . ".txt</file_name></file>";*/
					$this->orderID = $result [$i]['BBQOrderID'];
					$data_list = $data_list . "<child_order>" . $result [$i]['BBQOrderID'] . "</child_order><target_url><![CDATA[<?xml version=\"1.0\" encoding=\"utf-8\"?>"
												  .	"<All>";
					$readme_order = $readme_order . "<File><Remote>" . $host . $rooturl . "/orderinfo/createtxt/order/" . $result [$i]['BBQOrderID'] . '/' . $result [$i]['BBQOrderID'] . ".txt</Remote>"
												  . "<Local>" . date("Y-m-d",time()) . '/' . $result [$i]['BBQOrderID'] . '/' . $result [$i]['BBQOrderID'] . ".txt</Local></File>";
				}
				elseif ($this->_name == "BBQ_User_Files"){
					$file_name['cSrcFileName'] = $result [$i]['cSrcFileName'];
				}
				elseif ($this->_name =="BBQ_Files")
				{				
					echo $this->orderID;
						$Files = new Model_DbTable_Files ();
						$url = $Files->getUrlByMd5 ( $result [$i]['cMD5'] );
//						$file_list = $file_list . "<file><url>" . $url . "</url><file_name>" . $value[$i]['cSrcFileName'] . "</file_name><md5>" . $result [$i] ['cMD5'] . "</md5></file>";
						$file_list = $file_list . "<File><Remote>" . $url . "</Remote>"
												  . "<Local>" . date("Y-m-d",time()) . '/userfiles/'  . $value[$i]['cSrcFileName'] . "</Local></File>";
				}
			}
			$this->setPrimaryKey ( $result [$i],$file_name);
		}
		unset ( $result );
		$xml = $data_list . $file_list . $readme_order;
		return $xml;
	
	}
	
	public function getOrderType($orderid)
	{
		//订单类型(1数码印刷，2表示图文)
		
		$where = "ID = '" . $orderid . "'" ;
		$result = $this->fetchRow ( $where )->toArray ();
		$arr_result = array('BillSort'=>$result['BillSort'],'ClientUserID'=>$result['UserID']);
		return $arr_result;
	}
	
	public function getOrderFile($orderid) {
		/**
		 * 2011-2-11
		 * edit:ljx
		 * explain:修改函数getOrderXML()查询方式
		 * ex:
		 * SELECT * from MK_Evaluation_Main as m, MK_Evaluation_Sub as s, MK_Evaluation_Component_Sub as c, BBQ_User_Files as u, BBQ_Files as f
				    where m.ID = '27' and m.ID = s.BbqID and s.ID = c.SubID and c.FileID = u.nId and u.cFileMd5 = f.cMD5

		 */
		//begin    取表记录集
		$result = array ();
		$order_type = $this->getOrderType($orderid);
		
		$obj_select = $this->select ();		
		$obj_select->setIntegrityCheck ( false );
		mydbg($order_type['BillSort']);	
		switch ($order_type['BillSort'])
		{
			case 1:
				$obj_select->from ( array ('m' => 'MK_Evaluation_Main' ), '*' );
				$obj_select->join ( array ('s' => 'MK_Evaluation_Sub' ), 'm.ID = s.BbqID',array('ID','BbqID'));
				$obj_select->join ( array ('c' => 'MK_Evaluation_Component_Sub' ), 'c.SubID = s.ID', '*' );
				$obj_select->join ( array ('u' => 'BBQ_User_Files' ), 'c.FileID = u.nId', '*' );
				$obj_select->join ( array ('f' => 'BBQ_Files' ), 'u.cFileMd5 = f.cMD5', '*' );
				
				$obj_select->where ( " m.ID = ? ", $orderid );
				$obj_select->where(" c.FileID != -1");
				$obj_select->where ( " f.nStatus = 1 " );
				$obj_select->group ( "u.cFileMd5");
				$result = $this->fetchAll ( $obj_select )->toArray ();
				break;
			case 2:
				$obj_select->from ( array ('m' => 'MK_Evaluation_Main' ), '*' );
				$obj_select->join ( array ('pt' => 'MK_Evaluation_PicText' ), 'm.ID = pt.BbqID',array('ID','BbqID','FileID'));
				$obj_select->join ( array ('u' => 'BBQ_User_Files' ), 'pt.FileID = u.nId', '*' );
				$obj_select->join ( array ('f' => 'BBQ_Files' ), 'u.cFileMd5 = f.cMD5', '*' );
				
				$obj_select->where ( " m.ID = ? ", $orderid );
				$obj_select->where(" pt.FileID != -1");
				$obj_select->where ( " f.nStatus = 1 " );
				$obj_select->group ( "u.cFileMd5");
				$result = $this->fetchAll ( $obj_select )->toArray ();
				break;
			default:
				break;
		}
		mydbg(print_r($result,true));
		/**
		 * 1：会员没有上传文件,看成是会员索取样品,上传会员需求规格文件
		 * 2:会员上传了文件
		 */
		if (empty($result))
		{
			$obj_select = $this->select ();
			$obj_select->setIntegrityCheck ( false );
			$obj_select->from ( 'MK_Evaluation_Main', '*' );
			$obj_select->where ( "ID = '" . $orderid . "'" );
			$result = $this->fetchAll ( $obj_select )->toArray ();
		}
		
//		$result = $this->getListOrder ( $orderid );
		$data_list = "";
		$file_list = "";
		$readme_order = "";
		$file_name = array();
		$objUrl = new Model_Customize_CustFuns ();
		$rooturl = $objUrl->baseUrl ();
		$host = isset ( $_SERVER ['HTTP_X_FORWARDED_HOST'] ) ? $_SERVER ['HTTP_X_FORWARDED_HOST'] : (isset ( $_SERVER ['HTTP_HOST'] ) ? $_SERVER ['HTTP_HOST'] : '');
		$host = 'http://' . $host;
		for($i = 0; $i < count ( $result ); $i ++) {
//			if (($this->_name == "MK_Evaluation_Main") || ($this->_name == "BBQ_User_Files") || ($this->_name == "BBQ_Files")) {
//				if ($this->_name == "MK_Evaluation_Main") {
					if (empty($result [$i]['BBQOrderID']) || $result [$i]['BBQOrderID'] == null)
					{
						continue;
					}					
					$this->orderID = $result [$i]['BBQOrderID'];
					
					//规格说明文件
					if ($i == 0)
					{
						$data_list = $data_list . "<child_order>" . $result [$i]['BBQOrderID'] . "</child_order><target_url><![CDATA[<?xml version=\"1.0\" encoding=\"utf-8\"?>"
												  .	"<All>";
						$readme_order = $readme_order . "<File><Remote>" . $host . $rooturl . "/orderinfo/createtxt/order/" . $result [$i]['BBQOrderID'] . '/' . $result [$i]['BBQOrderID'] . ".txt</Remote>"
										. "<Local>" . date("Y-m-d",time()) . '/' . $result [$i]['BBQOrderID'] . '/' . $result [$i]['BBQOrderID'] . ".txt</Local></File></All>]]></target_url></order>";	
					}
					
//				}
					$Files = new Model_DbTable_Files ();
					$url = $Files->getUrlByMd5 ( $result [$i]['cMD5'] );
					$file_list = $file_list . "<File><Remote>" . $url . "</Remote>"
											. "<Local>" . date("Y-m-d",time()) . '/' . $result [$i]['BBQOrderID']  . '/' . $result[$i]['cSrcFileName'] . "</Local></File>";
//			}
//			$this->setPrimaryKey ( $result [$i],$file_name);
		}
		unset ( $result );
		$xml = $data_list . $file_list . $readme_order;
//		echo $xml;
		return $xml;
	
	}
	
	public function getOrderInfo($orderid) {
		$obj_select = $this->select ();
		$obj_select->setIntegrityCheck ();
		$obj_select->from ( array ('m' => $this->_name, array ('' ) ) );
		$obj_select->join ( array ('s' => 'MK_Evaluation_Sub' ), "m.ID = s.BbqID" );
		$obj_select->join ( array ('b' => 'MK_Evaluation_Component_Sub' ) );
	}
	public function setPrimaryKey($data,$value='') {
		if ($this->_name == "MK_Evaluation_PicText")
		{
			$this->arr_key[] = $data['FileID'];
		}
		if ($this->_name == "MK_Evaluation_Sub") {
			$this->arr_key [] = $data ['ID'];
		}
		if ($this->_name == "MK_Evaluation_Component_Sub") {
			$this->arr_key [] = $data ['FileID'];
		}
		if ($this->_name == "BBQ_User_Files") {
			$this->arr_key [] = $data ['cFileMd5'];
			$this->arr_value [] = $value;
		}
	}
	public function getOrderUserID($order){
		$selector = $this->select();
		$selector->where("BBQOrderID = '" . $order . "'");
//		$selector->where("BBQOrderID = 'B01028'");
		$selector->limit('1');
		$result = $this->fetchAll($selector);
		if ($result){
			return $result->toArray();
		}
		else {
		return false;
		}
	}

}

